//
//  JGHaveOrderController.m
//  JGRider
//
//  Created by 单启志 on 2018/1/25.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGHaveOrderController.h"
#import "JGHaveOrderCell.h"
#import "JGHaveOrderModel.h"
#import "JGHaveOrderApi.h"
@interface JGHaveOrderController ()<UITableViewDelegate,UITableViewDataSource,JGHaveOrderCellDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray<JGHaveOrderModel*> *datas;
@property(nonatomic,strong)JGHaveOrderApi *api;
@end

@implementation JGHaveOrderController
static NSString *cellId=@"haveOrderCell";
#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    [self configSubViews];
    [self.tableView.mj_header beginRefreshing];

    JGaddNotification(placeOrderSuccessNoti, PlaceOrderSuccessNoti, nil);
}

- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    NSLog(@"zj_viewWillAppearForIndex:%ld",index);
}
-(void)zj_viewDidAppearForIndex:(NSInteger)index{
    NSLog(@"zj_viewDidAppearForIndex:%ld",index);
}

- (void)zj_viewWillDisappearForIndex:(NSInteger)index{
    NSLog(@"zj_viewWillDisappearForIndex:%ld",index);
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    NSLog(@"zj_viewDidDisappearForIndex:%ld",index);
}

#pragma mark - private
-(void)configSubViews{
    self.title=@"已接单";
    self.datas=[JGHaveOrderModel models];
    [self.view addSubview:self.tableView];
    [self.tableView.mj_header beginRefreshing];
}
//下单成功通知 刷新
-(void)placeOrderSuccessNoti{
    [self.tableView.mj_header beginRefreshing];
}
-(void)getMoreData{
    [self getDataLoadMore:YES];
}
-(void)getNewData{
    [self getDataLoadMore:NO];
}
//
-(void)getDataLoadMore:(BOOL)isMore{

    FQWeakSelf(self);
    [self.api getHaveOrderListMid:LoginModel.Id isMore:isMore successBlock:^(id result) {

        [weakself refreshViewIsError:NO];
    } faileBlock:^(id error) {
        [self refreshViewIsError:YES];
        NSLog(@"已接单，一直请求吗");

    }];

   
}

-(void)refreshViewIsError:(BOOL)isError{
    FQWeakSelf(self);
    self.view.blankPageView.hidden=NO;
    [self.view configBlankPage:EaseBlankPageTypeView hasData:self.api.datas.count hasError:isError reloadButtonBlock:^(id sender) {
        [weakself.tableView.mj_header beginRefreshing];
    }];
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [self.tableView reloadData];
}
#pragma mark - tableview delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.api.datas.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//        return [tableView fd_heightForCellWithIdentifier:cellId configuration:^(JGHaveOrderCell *cell) {
//            cell.model=self.datas[indexPath.row];
//        }];
    return self.api.datas[indexPath.row].cellHeight;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    JGHaveOrderCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    cell.model=self.api.datas[indexPath.row];
    cell.delegate=self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


}

#pragma mark - cell delegate
-(void)cellShowAllEventWithCell:(UITableViewCell *)cell{

   NSInteger row=[self.tableView indexPathForCell:cell].row;

    self.api.datas[row].isShowAll=!self.api.datas[row].isShowAll;
    [self.tableView reloadRow:row inSection:0 withRowAnimation:UITableViewRowAnimationFade];

}

-(void)cellChangeOrderStatusEventWithCell:(UITableViewCell *)cell{

}

#pragma mark - getter setter
-(UITableView *)tableView{

    if (!_tableView) {

        _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0,0, JGScreenW, self.view.height-44-44-64) style:UITableViewStylePlain];

        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([JGHaveOrderCell class]) bundle:nil] forCellReuseIdentifier:cellId];
        _tableView.backgroundColor=HEXCOLOR(0xf2f2f2);
        _tableView.showsVerticalScrollIndicator=NO; _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];

        _tableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
    }

    return _tableView;

}

-(JGHaveOrderApi *)api{
    if (!_api) {
        _api=[[JGHaveOrderApi alloc]init];
    }
    return _api;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
